package com.example.waterdispenserm2.app;

import android.app.Application;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.text.TextUtils;

import com.example.waterdispenserm2.activity.MainActivity;
import com.example.waterdispenserm2.inter.MainInterfaceUtil;
import com.example.waterdispenserm2.util.MyLog;
import com.example.waterdispenserm2.util.SPUtils;
import com.example.waterdispenserm2.bean.M2UIItem;
import com.example.waterdispenserm2.dp.M2NoodleHelperUtil;
import com.example.waterdispenserm2.vmc.SerialService;


/*
 *     desc   :LPHYApplication
 *     version:  1
 *
 */
public class LPHYApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        SPUtils.init(getApplicationContext());
        if (M2NoodleHelperUtil.getInstance().getSQ().isEmpty()) {
            for (int i = 0; i < 9; i++) {
                M2UIItem m2UIItem = new M2UIItem();
                m2UIItem.setTag((i + 1) + "");
                m2UIItem.setSelect(false);
                M2NoodleHelperUtil.getInstance().setSQ(m2UIItem);
            }
        }


//        if (!TextUtils.isEmpty(SPUtils.getSerialPortNum())){
//            Intent serviceIntent = new Intent(this, SerialService.class);
//            bindService(serviceIntent, connection, Context.BIND_AUTO_CREATE);
//        }



        MyCrashHandler handler = new MyCrashHandler(this);
        Thread.setDefaultUncaughtExceptionHandler(handler);
        MyLog.e("Application","启动设备");
    }

    // 服务连接回调
    public static SerialService serialService;
    public static boolean bound = false;//是否连接
    public static final ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            SerialService.SerialBinder binder = (SerialService.SerialBinder) service;
            serialService = binder.getService();
            bound = true;
            MainInterfaceUtil.doCallBackUpdateConnectionStatus(bound);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            bound = false;
            MainInterfaceUtil.doCallBackUpdateConnectionStatus(bound);
        }
    };


}
